<?php

namespace app\service\admin\projects\video;

use app\model\Common;
use Qiniu\Auth;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 视频管理
 *
 * user ：Du
 * Date : 2022/3/31
 * Time : 11:09
 * Class VideoServices
 * @package projects\services\admin
 */
class VideoServices
{

    /**
     * 获取七牛上传token
     *
     * User : Du
     * Date : 2022/3/31
     * Time : 16:06
     * @return string
     */
    public static function getUploadToken(): string
    {
        $cfg = [
            'access' => 'zGV5mPiWlTMaVk-7wzrccYu7paV8_qa_v3PXwtjH',
            'secret' => 'lj5P-oXB2l1nFKaZI5etcac1uyb_lOmRi4_n4pfs',
            'bucket' => '1760',
            'domain' => 'http://img.lingyun.pro'
        ];
        $auth = new Auth($cfg['access'], $cfg['secret']);
        // 创建一个过期时间为1小时的临时上传令牌
        return $auth->uploadToken($cfg['bucket']);
    }

    /**
     * User : Du
     * Date : 2022/3/31
     * Time : 11:19
     * @param array $request
     * @param int $type
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public static function getList(array $request, int $type): array
    {
        $page = $request['page'] ?? 1;
        $size = $request['size'] ?? 10;
        switch ($type) {
            case 1:
                $res = Common::getAllData(VideoTypeTable, [], ['*']);
                break;
            case 3:
                $res = Common::getList(VideoInfoTable, [], ['*'], $page, $size);
                break;
            default:
                $res = [
                    'count' => 0,
                    'data' => [],
                ];
        }
        return $res;
    }

    /**
     * 添加数据
     *
     * User : Du
     * Date : 2022/3/31
     * Time : 11:40
     * @param array $request
     * @param int $type
     * @return false|int|string
     */
    public static function addData(array $request, int $type)
    {
        switch ($type) {
            case 1:
                $res = Common::addData(VideoTypeTable, $request);
                break;
            default:
                $res = false;
        }
        return $res;
    }

}